﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeoSketch2.Core
{
    /// <summary>
    /// A line with 2 points with double precision coordinates
    /// </summary>
    [Serializable]
    public class Line2D64D:BaseGeosketchObject
    {
        #region Fields
        /// <summary>
        /// The first point
        /// </summary>
        public Point2D64D Point1;

        /// <summary>
        /// The second point
        /// </summary>
        public Point2D64D Point2;
        #endregion

        #region Constructor
        /// <summary>
        /// A line with 2 points with double precision coordinates
        /// </summary>
        public Line2D64D()
            : this(new Point2D64D(), new Point2D64D())
        {
        }

        /// <summary>
        /// A line with 2 points with double precision coordinates
        /// </summary>
        /// <param name="Point1">The first point</param>
        /// <param name="Point2">The second point</param>
        public Line2D64D(Point2D64D Point1, Point2D64D Point2)
        {
            this.Point1 = Point1;
            this.Point2 = Point2;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets a line translated by a vector
        /// </summary>
        public Line2D64D GetTranslatedLine(Point2D64D TranslationVector)
        {
            return new Line2D64D(Point1 + TranslationVector, Point2 + TranslationVector);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the left normal vector, which goes to the left is we look from 1 to 2
        /// </summary>
        public Point2D64D LeftNormalVector
        {
            get
            {
                Point2D64D n = new Point2D64D(Point1.Y - Point2.Y, Point2.X - Point1.X);
                return n / n.Norm2D();
            }
        }

        /// <summary>
        /// Gets the right normal vector, which goes to the right is we look from 1 to 2
        /// </summary>
        public Point2D64D RightNormalVector
        {
            get
            {
                return -LeftNormalVector;
            }
        }

        /// <summary>
        /// Gets the middle of the line
        /// </summary>
        public Point2D64D Middle
        {
            get
            {
                return (Point1 + Point2) * 0.5;
            }
        }

        /// <summary>
        /// Gets the length of the line
        /// </summary>
        public double Length
        {
            get
            {
                return Point1.Distance2D(Point2);
            }
        }

        /// <summary>
        /// Gets the angle of the line from -pi to +pi
        /// </summary>
        public double Angle
        {
            get
            {
                if (Point2.Y >= Point1.Y)
                {
                    return Math.Acos((Point2.X - Point1.X) / this.Length);
                }
                else
                {
                    return -Math.Acos((Point2.X - Point1.X) / this.Length);
                }
            }
        }
        #endregion
    }
}
